package com.content.controller;

import com.content.model.dto.AddCourseDto;
import com.content.model.dto.CourseBaseInfoDto;
import com.content.model.dto.QueryCourseParamsDto;
import com.content.model.dto.UpdateCourseDto;
import com.content.model.groups.ValidationGroups;
import com.content.model.po.CourseBase;
import com.content.model.po.CourseTeacher;
import com.content.service.CourseBaseService;
import com.content.service.CourseTeacherService;
import com.content.base.model.PageParams;
import com.content.base.model.PageResult;
import com.content.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api(tags = "课程管理接口")
//@RequiredArgsConstructor
public class CourseBaseInfoController {

    @Autowired
    private CourseBaseService courseBaseService;
    @Autowired
    private CourseTeacherService courseTeacherService;
    @Value("${logging.config}")
    private String loggingConfig;

    @ApiOperation("课程查询接口")
    @PostMapping("course/list")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto) {
        System.out.println(loggingConfig);
        //取出用户身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        String companyId = null;
        if (user != null) {
            //机构id
            companyId = user.getCompanyId();
        }

        return courseBaseService.getCourseInfo(Long.parseLong(companyId), pageParams, queryCourseParamsDto);
    }

    @ApiOperation("新增课程接口")
    @PostMapping("/course")
    public CourseBaseInfoDto addCourseBase(@RequestBody @Validated(ValidationGroups.Inster.class) AddCourseDto addCourseDto) {
        //获取所属机构id
        return courseBaseService.addCourseBase(666L, addCourseDto);
    }

    @ApiOperation("回显课程相关信息")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getCourseBaseInfo(@PathVariable Long courseId) {
        //取出当前用户身份
        /*Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(principal);*/
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        System.out.println(user);

        return courseBaseService.getCourseInfoByCourseId(courseId);
    }

    @ApiOperation("修改课程接口")
    @PutMapping("/course")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated(ValidationGroups.Update.class) UpdateCourseDto updateCourseDto) {

        //根据课程id修改课程相关信息
        CourseBaseInfoDto courseBaseInfoDto = courseBaseService.updateByCourseId(updateCourseDto.getId(), updateCourseDto);

        return courseBaseInfoDto;
    }

    @ApiOperation("课程师资信息")
    @GetMapping("/courseTeacher/list/{courseId}")
    public List<CourseTeacher> getCourseTeachers(@PathVariable Long courseId) {
        return courseBaseService.getCourseTeachers(courseId);
    }

    @ApiOperation("添加课程师资信息")
    @PostMapping("/courseTeacher")
    public CourseTeacher addCourseTeacher(@RequestBody CourseTeacher courseTeacher) {
        CourseTeacher courseTeacher1 = courseTeacherService.addCourseTeacher(courseTeacher);
        return courseTeacher1;
    }

    @ApiOperation("删除课程师资信息")
    @DeleteMapping("/courseTeacher/course/{courseId}/{courseTeacherId}")
    public void deleteCourseTeacher(@PathVariable Long courseId, @PathVariable Long courseTeacherId) {
        courseTeacherService.removeById(courseTeacherId);
    }

    /**
     * 课程提交审核
     *
     * @param courseId
     */
    @PostMapping("/courseaudit/commit/{courseId}")
    public void commitAudit(@PathVariable("courseId") Long courseId) {
        Long companyId = 666L;
        courseBaseService.commitAudit(companyId, courseId);
    }
}
